
exports.invite = function(player, req, resp, onHandled) {
    do {
        var isAll = req.args.all ? 1:0;
        var isCash = req.args.cash ? 1:0;

        var user = player.user;
        if( user.tavern.roles.length >= 10 ) {
            resp.code = 1; resp.desc = 'tavern role full'; break;
        }

        var processes = [];
        var inviteNum = isAll ? 10-user.tavern.roles.length : 1;
        
        var cost = 0;
        if( isCash ) {
            var costType = 'cash';
            cost = +gConfGlobal.TavernCash; 
        }else {
            var costType = 'gold';
        }
        
        var firstEnrollFlag =  (1 == user.tavern.first) ? true : false;
        var costNum = 0;
        for( var i=0; i<inviteNum; i++ ) {
            if( isCash && user.tavern.level < 3 ) {
                user.tavern.level = 3; 
            }     

            var tavernConf = gConfTavern[user.tavern.level];
            if( !isCash && tavernConf ) {
                cost= +tavernConf.Gold;
            }

            if( user.status[costType]< (costNum +cost) ) {
                break;
            }
            costNum += cost;

            if( user.tavern.hasOwnProperty('special') && !user.tavern.special.got) {
                user.tavern.special.count += 1;
            }

            var roleQuantity = common.wRand(tavernConf.roles);
            // 第一次一键招聘蓝色和紫色将
            if( firstEnrollFlag && (7 == i) ) {
                roleQuantity = 'blue'; 
            }

            if( user.tavern.hasOwnProperty('special') && !user.tavern.special.got) {
                if( !user.tavern.first && roleQuantity == 'orange' ) {
                    // 招到橙将
                    user.tavern.special.got = 1;
                }else{
                    user.tavern.special.count += 1;    
                }
            }

            if( common.randRange(0, 100) < tavernConf.UpRate ) {
                user.tavern.level += 1;
            }else{
                if( isCash ) {
                    user.tavern.level = 3;
                }else {
                    user.tavern.level = 1;
                }
            }

            // 到头了
            if( !(user.tavern.level in gConfTavern) ) {
                if( isCash ) {
                    user.tavern.level = 3;
                }else {
                    user.tavern.level = 1;
                }
            }
            
            var specialInvite = false;
            if( user.tavern.hasOwnProperty('special') && !user.tavern.special.got && user.tavern.special.count >= (50 + player.uid%50) 
                && user.tavern.level >= 4 ) {
                roleQuantity = 'orange';
                user.tavern.special.got = 1;
                specialInvite = true;
            }
            
            var rid = 0;
            if( roleQuantity == 'orange' && gConfRole.orangeRole ) {
                if( !specialInvite ) {
                    rid = +common.wRand(gConfRole.orangeRole);
                }else{
                    rid = +common.wRand(gConfRole.orangeSpecialRole);
                }
            }else{
                var rids = gConfRole.rolesByQuantityAndReceiveMode[roleQuantity][1];
                rid = +rids[common.randRange(0, rids.length-1)];
            }
            
            // 第一次招将
            if( 1 == user.tavern.first ) {
                rid = gConfGlobal.FirstEnrollRoleId;
                user.tavern.first = 0;
            }

            processes.push([-cost, user.tavern.level, rid]);
            user.tavern.roles.push(rid);
        }
        if( processes.length <= 0 ) {
            resp.code = 1; resp.desc = 'not enough' + costType; break;
        }

        player.costResource(costType, costNum);
        player.markDirty('tavern');
        resp.data.process = processes;
    }while(false);

    onHandled();
}

exports.leave = function(player, req, resp, onHandled) {
    do {
        var user = player.user;

        if( user.tavern.roles.length <= 0 ) {
            resp.code = 1; resp.desc = 'no role to leave'; break;
        }

        var addSoul = 0;
        for( var i=0,max=user.tavern.roles.length; i<max; i++ ) {
            if( user.tavern.recruited.indexOf(i) >= 0 ) { // 0-9
                // 已经招募过了
                continue;
            }

            var rid = user.tavern.roles[i];
            addSoul += +gConfGlobal['RoleToSoul'+gConfRole[rid].Quantity];
        }

        user.status.soul += addSoul;
        player.markDirty('status.soul');

        user.tavern.roles = [];
        user.tavern.recruited = [];
        player.markDirty('tavern');

        resp.data.soul = addSoul;
    }while(false);

    onHandled();
}

exports.enroll = function(player, req, resp, onHandled) {
    do {
        if( isNaN(req.args.pos) ) {
            resp.code = 1; resp.desc = 'no valid args'; break;
        }

        var pos = +req.args.pos;

        var user = player.user;

        if( pos < 0 || pos >= user.tavern.roles.length ) {
            resp.code = 1; resp.desc = 'pos empty'; break;
        }

        if( user.tavern.recruited.indexOf(pos) >= 0 ) { // 0-9
            resp.code = 1; resp.desc = 'pos enrolled'; break;
        }

        var rid = user.tavern.roles[pos];
        var roleConf = gConfRole[rid];
        if( roleConf.Quantity == 'white' ) {
            resp.code = 1; resp.desc = 'white role'; break;
        }
        
        var role = player.addRole(rid);

        user.tavern.recruited.push(pos);
        player.markDirty('tavern.recruited');
        
        player.doTask('EnrollRole', roleConf.Quantity);
        player.checkEvent('DoTavernEnroll');

        if( roleConf.ReceiveWeight && roleConf.ReceiveWeight <= 10 ) {
            gMessage.addSystemMessage('enroll', [user.info.un, roleConf.Name]);
        }

        resp.data.role = role;
    }while(false);

    onHandled();
}

exports.recall = function(player, req, resp, onHandled) {
    do {
        if( isNaN(req.args.rid) || isNaN(req.args.num) ) {
            resp.code = 1; resp.desc = 'no valid args'; break;
        }

        var rid = +req.args.rid;
        var num = Math.round(req.args.num);
        var useGold = req.args.gold ? 1:0;

        if( num <= 0 || num > 10000 ) {
            resp.code = 1; resp.desc = 'invalid num'; break;
        }

        if( gConfRole.rolesByReceiveMode[4].indexOf(rid) < 0 ){
            resp.code = 1;
            resp.desc = 'rid not in exchange mode' ;
        }

        var user = player.user;
        var soulCost = 0;
        var goldCost = 0;
        if( useGold ) {
            goldCost = gConfGlobal.BuyOrangeRoleCostGold * num;
            if( !player.costResource('gold', goldCost) ) {
                resp.code = 1; resp.desc = 'not enough gold'; break;
            }
        }else{
            var quantity = gConfRole[rid].Quantity;
            soulCost = gConfGlobal["RoleRecallSoul"+quantity] * num;
            if( !player.costResource('soul', soulCost) ) {
                resp.code = 1; resp.desc = 'not enough soul'; break;
            }
        }
        
        player.addRoleCard(rid, num);

        resp.data.soul = -soulCost;
        resp.data.gold = -goldCost;
    }while(false);

    onHandled();
}
